package server;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.SpringLayout;

public class GUI extends JPanel {

	public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
	}
	
	public GUI() {
		BackEnd b = new BackEnd();
		
		JEditorPane serverLog = new JEditorPane();
		JScrollPane scroller = new JScrollPane(serverLog, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		
		JButton exitButton = new JButton("Exit");
		exitButton.addActionListener(new ActionListener() {
       	 
            public void actionPerformed(ActionEvent e)
            {
                //Execute when button is pressed
                System.out.println("Exiting server");
                System.exit(0);
            }
        }); 
		JPanel buttonPanel = new JPanel(new BorderLayout());
		buttonPanel.add(exitButton, BorderLayout.CENTER);
		
		JPanel right = new JPanel();
		right.setLayout(new BorderLayout());
		right.add(scroller, BorderLayout.CENTER);
		right.add(buttonPanel, BorderLayout.SOUTH);
		
		String[] users = b.getUsernames();
		JList userList = new JList(users);
		
		JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, userList, scroller);
		split.setDividerSize(3);
		this.setLayout(new BorderLayout());
		this.add(split, BorderLayout.CENTER);
		this.add(buttonPanel, BorderLayout.SOUTH);
		this.setVisible(true);
		this.setPreferredSize(new Dimension(400,600));
	}
	
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("KC Chat - Server");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create and set up the content pane.
        JPanel newContentPane = new GUI();
        frame.setContentPane(newContentPane);
		
/*        BufferedImage icon = null;
		try {
			icon = ImageIO.read(new File("icon-tran.png"));
		} 
		catch (IOException e) {
			e.printStackTrace();
		}
        
		frame.setIconImage(icon);*/

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }
	
}
